// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces fieldsfirst space 
// Source File Name:   Main.java

package image.util;

import java.io.*;
import java.util.*;
import java.util.logging.Logger;

// Referenced classes of package image.util:
//            ImageUtil

public class Main extends Observable {

    private static final Logger log = Logger.getLogger(Main.class.getName());
    private int totalFiles;
    private int processedFiles;
    private String error;
    private boolean stopped = true;
    private boolean stop = false;

    public Main() {
    }

    public static void main(String args[]) {
        Collection<String> strings;
        Map map;
        if (true) {
            return;
        }
        if (args.length < 3) {
            log.warning("params not enough");
            System.out.println("parameters: 1 - directory for jpeg scan, \n2 - target directory for saving, \n3 - new width of images \n4 - new heigh of images");
            System.out.println("or\nparameters: \n1 - directory for jpeg scan, \n2 - target directory for saving, \n3 - new maximum width or height of image ");
        } else {
            new Main().process(args);
        }
    }

    public void process(String args[]) {
        stop = false;
        stopped = false;
        try {
            long beginTime = System.currentTimeMillis();
            File src = new File(args[0]);
            File dst = new File(args[1]);
            if (!src.exists()) {
                throw new IllegalArgumentException("source directory not found");
            }
            if (!dst.exists() && !dst.mkdirs()) {
                throw new IllegalArgumentException("couldn't create target directory");
            }
            File images[] = src.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    String fileName = pathname.getName().toLowerCase();
                    return fileName.endsWith(".jpg") || fileName.endsWith(".jpeg");
                }
            });
            error = null;
            processedFiles = 0;
            totalFiles = images.length;
            setChanged();
            notifyObservers();
            for (File image : images) {
                if (stop) {
                    break;
                }
                long t1 = System.currentTimeMillis();
                File fdst = new File(dst.getAbsolutePath() + File.separator + image.getName());
                log.info("start processing file: " + image.getName());
                JPEGResizer resizer = new JPEGResizer(image, fdst);
                if (args.length != 3) {
                    resizer.setWidth(Integer.parseInt(args[2]));
                    resizer.setHeight(Integer.parseInt(args[3]));
                } else {
                    resizer.setMaxSize(Integer.parseInt(args[2]));
                }
                resizer.resize();
                long t2 = System.currentTimeMillis();

                log.info("\tfile processed");
                log.info("\telapsed " + (t2 - t1) + " milliseconds");
                processedFiles++;
                setChanged();
                notifyObservers();
            }

            long endTime = System.currentTimeMillis();
            log.info((new StringBuilder()).append("total time elapsed: ").append(endTime - beginTime).toString());
        } catch (Exception e) {
            log.severe(e.toString());
            error = e.getMessage();
            setChanged();
            notifyObservers();
        }

        stopped = true;
    }

    public void stop() {
        stop = true;
    }

    public boolean isStopped() {
        return stopped;
    }

    public int getTotalFiles() {
        return totalFiles;
    }

    public int getProcessedFiles() {
        return processedFiles;
    }

    public String getError() {
        return error;
    }

}
